<?php
namespace Home\Controller;
use Think\Controller;
class MessageController extends ZMController {
	private static $apiKey='Z0cdoCXULiMdCx9k';//秘钥
	/*public function _initialize(){
		S(array('expire'=>691200));//8小时
		if(!session('?user')){
			$pdate=I('pdate');//秘钥时间
			$sapiKey=I('apiKey');//对方数字签名
			$signature=base64_encode(md5($pdate.self::$apiKey));
			if($sapiKey!=$signature){//对比失败
				$this->errorreturn("apiKey错误");
			}
		}
	}*/
	/*医院数据*/
	
	public function yearTypeCollect($r=NULL){//每个种类的年收集
		$region=session('user.region');
		$Model=M()->db(1,'DB_CONFIG1');
		if($region===NULL){
			$region=$r;
		}
		if($region===NULL){//取session值
			$region=I('region');//靠post获取
		}
		if($region===NULL){
			exit(0);
		}
		$hid=NULL;
		if($region!=999){//区域
			$arr=$Model->query("select hid from ddwk_hospital where region=$region and hid<>11");
			foreach($arr as $re){
				$hid.=','.$re['hid'];
			}
			$hid=ltrim($hid,',');
		}
		$startdate=date('Y-01-01');
		$enddate=date('Y-m-d',strtotime('-1 day'));
		$collect=array();
		$arr=array(0,1,2,3,4,5,6,7);
		$collectOver=R('Report/getCollectOver',array($startdate,$enddate,$hid));
		$count=array();
		foreach($collectOver as $v){
			$count['weight_0']+=$v['totalweight'];
			$count['weight_1']+=$v['weight_0'];
			$count['weight_2']+=$v['weight_1'];
			$count['weight_3']+=$v['weight_3'];
			$count['weight_4']+=$v['weight_4'];
			$count['weight_5']+=$v['weight_5'];
			$count['weight_6']+=$v['weight_6'];
			$count['weight_7']+=$v['weight_7'];
		}
		foreach($arr as $a){
			$collect[$a]=$count['weight_'.$a];
		}
		if(ACTION_NAME=='yearTypeCollect'){
			$this->ajax($collect);
		}
		else{
			return $collect;
		}
	}
	
	//今年所有医院各月收集(最近6个月)
	public function monthCollect(){
	    $region=session('user.region');
		if($region===NULL){//取session值
			$region=I('region');//靠post获取
		}
		if($region===NULL){
			exit(0);
		}
		$Model=M()->db(1,'DB_CONFIG1');
		$hid=NULL;
		if($region!=999){//区域
			$arr=$Model->query("select hid from ddwk_hospital where region=$region and hid<>11");
			foreach($arr as $re){
				$hid.=','.$re['hid'];
			}
			$hid=ltrim($hid,',');
		}
		$i=0;
		$collect=array();
		$c=array();
		while($i<6){
			$month=getMonth($i);
			$y=date('Y',strtotime($month));
			$m=date('m',strtotime($month));
			$d=checkdays($y,$m);
			$s=$y.'-'.$m.'-01';
			$e=$y.'-'.$m.'-'.$d;
			$collectOver=R('Report/getCollectOver',array($s,$e,$hid,$region));
			$count=array();
			foreach($collectOver as $v){
				$count['totalweight']+=$v['totalweight'];
			}
			$collect[$month]=$count['totalweight'];
			$c[]=$collect;
			unset($collect);
			$i++;
		}
		$c=array_reverse($c);
		$this->ajax($c);
	}
	public function nowCollect(){//实时收集情况
		$Model=M()->db(1,'DB_CONFIG1');
		$region=session('user.region');
		if($region===NULL){
			exit(0);
		}
		$where='';
		if($region!=999){//区域
			$arr=$Model->query("select hid from ddwk_hospital where region in ($region) and hid<>11");
			foreach($arr as $re){
				$hid.=','.$re['hid'];
			}
			$hid=ltrim($hid,',');
			$where.=" and hid in ($hid)";
		}
		$r=$Model->query("select hid from ddwk_hospital where 1=1 $where");
		$hid='';
		foreach($r as $v){
			$hid.=','.$v['hid'];
		}
		$hid=ltrim($hid,',');
		$res=$Model->query("select cid,ctime,hid,areaid from ddwk_collect where hid in ($hid) order by cid desc limit 0,8");
		foreach($res as &$row){
			$cid=$row['cid'];
			$row['weight']=R('Collect/totalCollectweight',array($cid));//重量
			$hid=$row['hid'];
			$areaid=$row['areaid'];
			$row['hospname']=R('Cust/hospname',array($hid));//医院名称
			$row['area']=R('Cust/areaname',array($areaid));//科室名称
		}
		$this->ajax($res);
	}
	public function regionDayCollect(){//区域昨日收集
		$dir='./Public/regionDayLock.txt';
		$fp=R('Lock/openFile',array($dir));
		$LOCK=R('Lock/getLock',array($fp));
		if($LOCK){
			$arr=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);//区
			$collect=array();
			$pdate=date('Y-m-d',strtotime("-1 day"));
			foreach($arr as $v){
				$collect[$v]=$this->getDayCollect($pdate,$v);
			}
			$this->ajax($collect);
			$release=R('Lock/releaseLock',array($fp));//释放锁
		}
		else{
			echo '等待';
		}
		R('Lock/closeFile',array($fp));
	}
	private function getDayCollect($date,$region){//区域日收集
		$dir='./Public/regioncollect/';
		if(!file_exists($dir)){//创建路径
			mkdir($dir,0777,true);
		}
		$file=$dir.$region.'.txt';
		$fp=fopen($file,'a+');
		$content=file_get_contents($file);
		$content= explode("\r\n",$content);//text内容数组
		$pdate=$content['0'];//最近更新日期
		array_shift($content);//移除数组第一个元素
		if($pdate==date('Y-m-d')){
			array_pop($content);
			return $content;
		}
		else{//更新
			$Model=M()->db(1,'DB_CONFIG1');
			$arr=$Model->query("select hid from ddwk_hospital where region in ($region) and hid<>11");
			if(empty($arr)){
				return array();
			}
			foreach($arr as $re){
				$hid.=','.$re['hid'];
			}
			$hid=ltrim($hid,',');
			$arr=array(0,1,2,3,4,5,6);
			$a=array();
			$Model=M()->db(1,'DB_CONFIG1');
			$res=$Model->query("select cid from ddwk_collect where pdate='$date' and hid in ($hid)");//后一天
			$cid='';
			foreach($res as $v){
				$cid.=','.$v['cid'];
			}
			$cid=ltrim($cid,',');
			foreach($arr as $v){
				$a[$v]=$this->dayCollectHosp($cid,$v);
				$a[$v]=$a[$v]."\r\n";
			}
			$nowdate=date('Y-m-d');
			array_unshift($a,$nowdate."\r\n");
			file_put_contents($file,$a);
			fclose($fp);
			array_shift($a);
			return $a;
		}
	}
	public function regionDayHosp(){//区域各个医院收集数据
		$region=session('user.region');//区域
		if($region===NULL){
			exit(0);
		}
		if($region==999){
			return ;
		}
		$Model=M()->db(1,'DB_CONFIG1');
		$arr=$Model->query("select hid,lnglat from ddwk_hospital where region in ($region) and hid<>11");
		$LngLat=array();
		$hid='';
		foreach($arr as $r){
			$m=$r['hid'];
			$LngLat[$m]=$r['lnglat'];
			$hid.=','.$m;
		}
		$hid=ltrim($hid,',');
		$hid=explode(',',$hid);
		$date=date('Y-m-d');
		$a=array();
		$type=array(0,1,2,3,4,5,6);
		foreach($hid as $v){
			$arr['name']=R('Cust/hospname',array($v));//医院名
			$arr['LngLat']=$LngLat[$v];
			$res=$Model->query("select cid from ddwk_collect where pdate='$date' and hid in ($v)");//后一天
			$cid='';
			foreach($res as $v){
				$cid.=','.$v['cid'];
			}
			$cid=ltrim($cid,',');
			foreach($type as $t){
				$arr[$t]=$this->dayCollectHosp($cid,$t);
			}
			$a[]=$arr;
		}
		$this->ajax($a);
	}
	public function dayCollectHosp($cid,$type){//医院日收集
		try{
			if(empty($cid)){
				E('参数错误');
			}
			if($type>1){
				$type+=1;
			}
			$weight=R('Collect/totalCweight',array($cid,$type));
			return $weight;
		}
		catch(\Exception $e){
			$message=$e->getMessage();
			return 0;
		}
	}
	public function hospCollect(){//医院昨日收集
	    $Model=M()->db(1,'DB_CONFIG1');
		$region=session('user.region');
		if($region===NULL){
			exit(0);
		}
		$where='';
		if($region!=999){//区域
			$arr=$Model->query("select hid from ddwk_hospital where region=$region and hid<>11 and img=1");
			foreach($arr as $re){
				$hid.=','.$re['hid'];
			}
			$hid=ltrim($hid,',');
			$where.=" and hid in ($hid)";
		}
		$h=$Model->query("select hid from ddwk_hospital where hid<>11 $where and img=1");
		$hid='';
		foreach($h as $v){
			$hid.=','.$v['hid'];
		}
		$hid=ltrim($hid,',');
		$h=explode(',',$hid);
		$count=count($h);
		if($count>4){
		    $key=array_rand($h,4);
		}
		else{
			$a=0;
			$key=array();
			while($a<$count){
				array_push($key,$a);
				$a++;
			}
		}
		$date=date('Y-m-d',strtotime('-1 day'));
		$arr=array(0,1,2,3,4,5,6);
		$a=array();
		$b=array();
		foreach($key as $v){
			$hid=$h[$v];
			$i=0;
			$res=$Model->query("select cid from ddwk_collect where pdate='$date' and hid in ($hid)");//后一天
			$cid='';
			foreach($res as $v){
				$cid.=','.$v['cid'];
			}
			$cid=ltrim($cid,',');
			foreach($arr as $r){
				$a[$r]=$this->dayCollectHosp($cid,$r);
				$i+=1;
			}
			$a[$i]=R('Cust/hospname',array($hid));
			$a[$i+1]=$hid;
			$b[]=$a;
		}
		$this->ajax($b);
	}
}